#include "textwidget.h"
#include "ui_textwidget.h"

textWidget::textWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::textWidget)
{
    ui->setupUi(this);

    recv_data_len = 0;
    send_data_len = 0;

    connect(ui->pushButton_2,&QPushButton::clicked,this,[=](){ui->textEdit_2->clear();});  //清除发送区域
    connect(ui->pushButton_4,&QPushButton::clicked,this,[=](){ui->textEdit->clear();});  //清除接收区域
    connect(ui->pushButton_3,&QPushButton::clicked,this,[=](){recv_data_len = 0; send_data_len = 0;

         ui->label->setText(QString("%1").arg(send_data_len));
         ui->label_4->setText(QString("%1").arg(recv_data_len));
    });  //清除计数

    connect(ui->pushButton,&QPushButton::clicked,this,[=](){
        QString text = ui->textEdit_2->toPlainText();

        if(ui->checkBox_2->isChecked())
            text += "\r\n";
        send_data_len += text.toUtf8().size();
        ui->label->setText(QString("%1").arg(send_data_len));
        emit textdisplay_SendData(text.toUtf8().data(),text.toUtf8().size());
    });
}

textWidget::~textWidget()
{
    delete ui;
}

//显示接收文本区域
void textWidget::textdisplay_RecvData(QByteArray data,QString hex_string){
    recv_data_len += data.size();
    ui->label_4->setText(QString("%1").arg(recv_data_len));
    if(ui->checkBox->isChecked() == true){
        ui->textEdit->insertPlainText(hex_string);
    }else{
        ui->textEdit->insertPlainText(QString::fromUtf8(data));
    }
    ui->textEdit->moveCursor(ui->textEdit->textCursor().End);
}
